const pub = {
	go: 'ok',
	fun() {
		return '好'
	},
	// 正则验证方法
	// 手机号验证
	isMobile: (str) => {
		return /^1[3456789]\d{9}$/.test(str);
	},
	// 验证url
	isUrl: (str) => {
		return /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)/
			.test(str);
	},
	// 验证身份证号
	isPersonCard: (card) => {
		console.log(card)
		return /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/.test(card)
	},
	// 密码验证
	pwd1: (password) => {
		return /^(\w){6,16}$/.test(password);
	},
	// 字母数字和符号
	pwd16: (password) => {
		return /^(\w){1,16}$/.test(password);
	},
	pwd50: (password) => {
		return /^(\w){1,50}$/.test(password);
	},
	// 字母数字和符号
	pwd30: (password) => {
		return /^(\w){1,30}$/.test(password);
	},
	// 字母数字和符号
	pwd32: (password) => {
		return /^(\w){1,32}$/.test(password);
	},
	// 字母数字和符号
	pwd100: (password) => {
		return /^(\w){1,100}$/.test(password);
	},

	//获取当前日期时间格式化的字符串
	getCurrentDateTime: (timeStr) => {
		function g(t) {
			if (t < 10) {
				t = '0' + t;
			}
			return t;
		}
		Date.prototype.toLocaleString = function() {
			return this.getFullYear() + "-" + g(this.getMonth() + 1) + "-" + g(this.getDate()) + " " + g(
				this.getHours()) + ":" + g(this.getMinutes()) + ":" + g(this.getSeconds());
		};
		var str = timeStr ? timeStr : new Date().getTime();
		var time = new Date(str);
		time = time.toLocaleString();
		return time;
	},
	//获取当前日期格式化的字符串
	getCurrentDate: (timeStr) => {
		function g(t) {
			if (t < 10) {
				t = '0' + t;
			}
			return t;
		}
		Date.prototype.toLocaleString = function() {
			return this.getFullYear() + "-" + g(this.getMonth() + 1) + "-" + g(this.getDate());
		};
		var str = timeStr ? timeStr : new Date().getTime();
		var time = new Date(str);
		time = time.toLocaleString();
		return time;
	},
	//获取当前时间格式化的字符串
	getCurrentTime: (timeStr) => {
		function g(t) {
			if (t < 10) {
				t = '0' + t;
			}
			return t;
		}
		Date.prototype.toLocaleString = function() {
			return g(this.getHours()) + ":" + g(this.getMinutes()) + ":" + g(this.getSeconds());
		};
		var str = timeStr ? timeStr : new Date().getTime();
		var time = new Date(str);
		time = time.toLocaleString();
		return time;
	},
	// 获取当前日期
	getDate(type) {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	},
	// app内区域截图利用html2Canvals保存到手机
	saveDomImage(html2Canvas, dom, fileName) { //使用之前要引入 html2Canvas.js  
		if (mui.os.plus) {
			if (typeof html2Canvas == null)
				throw Error("html2Canvas is not defined");
			if (dom == null)
				throw Error("saveDomImage param : dom is null");
			if (fileName == null || fileName == "")
				fileName = "untitled.png";

			var getPixelRatio = function(context) {
				var backingStore = context.backingStorePixelRatio ||
					context.webkitBackingStorePixelRatio || 1;
				return (window.devicePixelRatio || 1) / backingStore;
			};

			var _canvas = document.createElement('canvas');

			var ctx = _canvas.getContext('2d');
			var ratio = getPixelRatio(ctx);
			ctx.scale(ratio, ratio);

			var w = dom.offsetWidth;
			var h = dom.offsetHeight;

			_canvas.width = w;
			_canvas.height = h;
			_canvas.style.width = w * ratio + 'px';
			_canvas.style.height = h * ratio + "px";

			html2Canvas(dom, {
				allowTaint: true,
				logging: false,
				profile: true,
				useCROS: true,
				canvas: _canvas,
				onrendered: function(canvas) {
					var dataUrl = canvas.toDataURL();
					var b = new plus.nativeObj.Bitmap('bitblmap');

					b.loadBase64Data(dataUrl, function() {
						/*这里一定要是_doc目录*/
						b.save("_doc/" + fileName, {
							overwrite: true
						}, function(object) {
							//保存到相册  
							plus.gallery.save("_doc/" + fileName, function() {
								mui.toast("图片已保存到相册");
							}, function() {
								mui.toast("图片保存失败");
							});
						}, function() {
							mui.toast("图片保存失败");
						});
					}, function() {
						mui.toast("图片保存失败");
					});
				}
			});
		}
	},
	/**
	 * 根据身份证号得到姓别和精确计算年龄
	 */
	analyzeIDCard(IDCard) {
		var sexAndAge = {};
		//获取用户身份证号码
		var userCard = IDCard;
		//如果身份证号码为undefind则返回空
		if (!userCard) {
			return sexAndAge;
		}
		//获取性别
		if (parseInt(userCard.substr(16, 1)) % 2 == 1) {
			sexAndAge.sex = '1' // 男
		} else {
			sexAndAge.sex = '0' // 女
		}
		//获取出生年月日
		//userCard.substring(6,10) + "-" + userCard.substring(10,12) + "-" + userCard.substring(12,14);
		var yearBirth = userCard.substring(6, 10);
		var monthBirth = userCard.substring(10, 12);
		var dayBirth = userCard.substring(12, 14);
		//获取当前年月日并计算年龄
		var myDate = new Date();
		var monthNow = myDate.getMonth() + 1;
		var dayNow = myDate.getDay();
		var age = myDate.getFullYear() - yearBirth;
		if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
			age--;
		}
		//得到年龄
		sexAndAge.age = age;
		//返回性别和年龄
		return sexAndAge;
	},
	compage(bir) {
		let birthday = bir.replace(/\-/g, "");
		var sexAndAge = {};
		//获取出生年月日
		//userCard.substring(6,10) + "-" + userCard.substring(10,12) + "-" + userCard.substring(12,14);
		var yearBirth = birthday.substring(0, 4);
		var monthBirth = birthday.substring(4, 6);
		var dayBirth = birthday.substring(6, 8);
		//获取当前年月日并计算年龄
		var myDate = new Date();
		var monthNow = myDate.getMonth() + 1;
		var dayNow = myDate.getDay();
		var age = myDate.getFullYear() - yearBirth;
		if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
			age--;
		}
		//得到年龄
		sexAndAge.age = age;
		//返回性别和年龄
		return sexAndAge;
	},

}

export default pub;
